package com.mezz.bones.im.gateway.handler;
import com.mezz.bones.im.gateway.service.IUserChannelService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author mezz
 * @date 2022-09-23 10:46
 * @description:
 */
@Slf4j
@Component
@ChannelHandler.Sharable
@AllArgsConstructor
public class HandshakeHandler extends ChannelInboundHandlerAdapter {

  private final IUserChannelService userChannelService;

  @Override
  public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    if (evt instanceof WebSocketServerProtocolHandler.HandshakeComplete) {
      log.info("web socket 握手成功。");
      userChannelService.addUnAuthChannel(ctx);
      return;
    }

    super.userEventTriggered(ctx, evt);

  }
}
